<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-26
 * Time: 18:06
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\model\User;
use app\base\validate\PubValidate;
use think\Request;

class Usermanagement extends BaseController
{
    /***
     * 输出用户管理页面视图
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }
    /***
     * 输出用户管理页面数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ajax()
    {

        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }
        $user = new User();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $user->count('user_id');
        $data = $user->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }
    /***
     * 删除用户接口
     * @return array
     * @throws \think\exception\DbException
     */
    public function dell()
    {
        $request = Request::instance();
        $userid = $request->has('user_id')?$request->param('user_id'):false;
        if (!$userid) {
            return ['code' => -1, 'msg' => '参数错误'];
        }

        $user_img=User::get($userid)->user_img;
        if ($user_img!='/static/header-img/head.jpg') {
            unlink(ROOT_PATH.'public'.$user_img);
        }

        if (User::destroy($userid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }
    /***
     * 编辑用户接口
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = new PubValidate();
            if (!$validate->scene('edit')->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }

            $uid= User::where('user_name',$data['user_name'])->find();
            if ($uid->user_id=='1' && $data['user_type']!='1') {
                return ['code'=>-1,'msg'=>'超级管理员权限不能更改'];
            }

            if ($data['user_img']!=$uid->user_img && $uid->user_img !='/static/header-img/head.jpg') {
                unlink(ROOT_PATH.'public'.$uid->user_img);
            }

            if (empty($data['user_pwd'])) {
                User::where('user_name', $data['user_name'])->update([
                    'user_nick' => $data['user_nick'],
                    'user_type' => $data['user_type'],
                    'user_img' => $data['user_img'],
                ]);
            }else{
                if ($data['user_pwd']!=$data['user_rpwd']) {
                    return ['code'=>-1,'msg'=>'两次密码输入不一致'];
                }
                User::where('user_name', $data['user_name'])->update([
                    'user_nick' => $data['user_nick'],
                    'user_pwd' => md5($data['user_pwd']) ,
                    'user_type' => $data['user_type'],
                    'user_img' => $data['user_img'],
                ]);
            }
            return ['code'=>1,'msg'=>'修改成功'];
        }
        $userinfo= User::get($request->param('user_id'));

        $this->assign('user_name', $userinfo->user_name);
        $this->assign('user_nick', $userinfo->user_nick);
        $this->assign('user_type', $userinfo->user_type);
        $this->assign('user_img', $userinfo->user_img);
        return $this->fetch();
    }
    /***
     * 增加用户接口
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function adduser()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = new PubValidate();
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }

            $user = User::get(['user_name' => $request->param('user_name')]);
            if (!is_null($user)) {
                return ['code'=>-1,'msg'=>'账号已存在'];
            }
            User::create([
                'user_name' => $data['user_name'],
                'user_pwd' => md5($data['user_pwd']),
                'user_nick' => $data['user_nick'],
                'user_type' => $data['user_type'],
                'user_img' => $data['user_img'],

            ]);
            return ['code'=>1,'msg'=>'添加成功'];
        }
        return $this->fetch();
    }
    /***
     * 图像上传接口
     * @return array
     */
    public function upload(){

        $file = $this->request->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static/header-img');
        return ['code' => 0, 'msg' => '上传成功', 'data' => ['src' =>'/static/header-img/'.$info->getSaveName()]];
    }



}